package com.onesports.intelligent.k12.polarlight.service.report;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.onesports.framework.kit.common.util.BeanUtils;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.OrgPageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.report.OperationAccountantQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.report.OrganizationAccountantQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.SettlementOrderSelectVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.TotalDataVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.report.OperationAccountantVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.report.OrganizationAccountantVO;
import com.onesports.intelligent.k12.polarlight.mapper.market.OrdersSubMapper;
import com.onesports.intelligent.k12.polarlight.util.CalculateUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 收支统计
 */
@Service
@RequiredArgsConstructor
public class AccountantService {

    private final OrdersSubMapper ordersSubMapper;

    /**
     * 机构端查询
     * @param dto
     * @param organizationId
     * @return
     */
    public TotalDataVO<OrganizationAccountantVO> selectOrganizationPage(OrganizationAccountantQueryDTO dto,String organizationId) {
        OperationAccountantQueryDTO params = BeanUtils.copyProperties(dto,OperationAccountantQueryDTO.class);
        params.setOrganizationId(organizationId);
        IPage<OperationAccountantVO> page = ordersSubMapper.selectAccountant(dto.getPager(),params);
        IPage<OrganizationAccountantVO> orgPage = page.convert(item-> BeanUtils.copyProperties(item,OrganizationAccountantVO.class));
        OperationAccountantVO operationTotalData = ordersSubMapper.selectAccountantSumValue(params);
        OrganizationAccountantVO totalData = BeanUtils.copyProperties(operationTotalData,OrganizationAccountantVO.class);
        TotalDataVO<OrganizationAccountantVO> totalDataVO = new TotalDataVO<>();
        totalDataVO.setTotalData(totalData);
        totalDataVO.setPage(orgPage);
        return totalDataVO;
    }

    /**
     * 运营端查询
     * @param dto
     * @return
     */
    public TotalDataVO<OperationAccountantVO> selectOperationPage(OperationAccountantQueryDTO dto) {
        IPage<OperationAccountantVO> page = ordersSubMapper.selectAccountant(dto.getPager(),dto);
        OperationAccountantVO totalData = ordersSubMapper.selectAccountantSumValue(dto);
        TotalDataVO<OperationAccountantVO> totalDataVO = new TotalDataVO<>();
        totalDataVO.setTotalData(totalData);
        totalDataVO.setPage(page);
        return totalDataVO;
    }

    /**
     * 机构端导出
     * @param dto
     * @param organizationId
     * @return
     */
    public List<OrganizationAccountantVO> organizationExport(OrgPageOrderDTO dto,String organizationId) {
        OperationAccountantQueryDTO params = BeanUtils.copyProperties(dto,OperationAccountantQueryDTO.class);
        params.setOrganizationId(organizationId);
        List<OperationAccountantVO> list = ordersSubMapper.selectAccountant(params);
        OperationAccountantVO totalData = ordersSubMapper.selectAccountantSumValue(params);
        if(null!=totalData){
            totalData.setCampusName("合计：");
            list.add(totalData);
        }
        if(CollectionUtils.isNotEmpty(list)){
            return list.stream().map(item->BeanUtils.copyProperties(item,OrganizationAccountantVO.class)).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 运营端导出
     * @param dto
     * @return
     */
    public List<OperationAccountantVO> operationExport(OperationAccountantQueryDTO dto) {
        List<OperationAccountantVO> list = ordersSubMapper.selectAccountant(dto);
        OperationAccountantVO totalData = ordersSubMapper.selectAccountantSumValue(dto);
        if(null!=totalData){
            totalData.setCampusName("合计：");
            list.add(totalData);
        }
        return list;
    }
}
